//
//  FavoriteViewController.m
//  体育
//
//  Created by  on 2022/10/24.
//

#import "FavoriteViewController.h"
#import "FavoriteCell.h"

@interface FavoriteViewController ()
@property (strong, nonatomic) IBOutlet UIView *toolbar;
@property (strong, nonatomic) IBOutlet UIButton *selectAll;
@property (strong, nonatomic) IBOutlet UILabel *totalNum;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *toolbarBottom;
@property (strong, nonatomic)  NSArray *array;
@property (strong, nonatomic)  NSMutableArray *selectArray;

@end

@implementation FavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = LS(@"收藏");
    [self loadData];
    [NotifyTool add:self :@selector(loadData) :notifyRecordUpdate];
    [self.rightBtn setTitle:LS(@"管理") forState:UIControlStateNormal];
    [self.rightBtn setTitle:LS(@"取消") forState:UIControlStateSelected];
    [self.tableView registerNib:[FavoriteCell nib] forCellReuseIdentifier:[FavoriteCell ID]];
    //    self.tableView.c_edges.equalTo(self.view);
    self.tableView.c_left.equalTo(self.view).coffset(0);
    self.tableView.c_right.equalTo(self.view).coffset(0);
    self.tableView.c_top.equalTo(self.view).coffset(0);
    self.tableView.c_bottom.equalTo(self.toolbar.c_top).coffset(0);
    [self.view bringSubviewToFront:self.toolbar];
}
- (void)loadData{
    [DataTool findLike:YES complete:^(NSMutableArray<Record *> * _Nonnull result) {
        self.array = result;
        self.selectArray = [NSMutableArray array];
        for (int i = 0; i<self.array.count; i++) {
            [self.selectArray addObject:@"0"];
        }
        [self.tableView reloadData];
    }];
}
-(void)rightBtnAction{
    self.rightBtn.selected = !self.rightBtn.selected;
    self.selectAll.selected = self.rightBtn.selected;
    self.toolbarBottom.constant = self.rightBtn.selected ? -12 : -82;
    self.selectArray = [NSMutableArray array];
    for (int i = 0; i<self.array.count; i++) {
        [self.selectArray addObject:self.rightBtn.selected?@"1":@"0"];
    }
    [self.tableView reloadData];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        sender.selected = !sender.selected;
        self.selectArray = [NSMutableArray array];
        for (int i = 0; i<self.array.count; i++) {
            [self.selectArray addObject:sender.selected?@"1":@"0"];
        }
    }else{
        NSMutableArray *array = [NSMutableArray array];
        NSMutableArray *array2 = [NSMutableArray array];
        for (int i = 0; i < self.array.count; i++) {
            BOOL select = [self.selectArray[i] boolValue];
            Record * model = self.array[i];
            if (select && model.favorite) {
                [DataTool delete:model complete:^(BOOL success) {
                }];
            }else{
                [array addObject:model];
                [array2 addObject:@"0"];
            }
        }
        self.array = array;
        self.selectArray = array2;
    }
    [self.tableView reloadData];
}
- (void)push:(Record *)model{
    if (model.imgs.count <= 0) {
        FeedBackViewController *vc = [FeedBackViewController new];
        vc.model = model;
        vc.title = LS(@"备注");
        [[self currentVC] push:vc :YES];
    }else{
        CheckViewController *vc = [CheckViewController new];
        vc.model = model;
        [self push:vc :YES];
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:[FavoriteCell ID]];
    cell.model =  self.array[indexPath.section];
    cell.selectBtn.hidden = !self.rightBtn.selected;
    cell.selectBtn.selected = [self.selectArray[indexPath.section] boolValue];
    WS(self)
    cell.block = ^(UIButton * _Nonnull button) {
        if (button.tag == 201) {
            weakself.selectArray[indexPath.section] = button.selected ? @"1":@"0";
        }else{
            [weakself push:weakself.array[indexPath.section]];
        }
    };
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [DetailViewController new];
    vc.model = self.array[indexPath.section];
    [self push:vc :YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == self.array.count - 1 ? 20 : 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == self.array.count - 1){
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        return view;
    }else{
        return nil;
    }
}


@end
